package cn.xkai.exercise.a;

import com.google.gson.Gson;

/**
 * @description: 两数之和 II - 输入有序数组
 * 自己的思路：利用双指针对有序数组前后进行相加比较目标值
 * 借鉴的思路：同理
 * @author: kaixiang
 * @date: 2022/6/26
 **/
public class Solution14 {
    public int[] twoSum(int[] numbers, int target) {
        int left = 0, right = numbers.length - 1;
        while (left < right) {
            if (numbers[left] + numbers[right] == target) {
                break;
            }
            if (numbers[left] + numbers[right] < target) {
                left++;
            } else {
                right--;
            }
        }
        return new int[]{left + 1, right + 1};
    }

    public static void main(String[] args) {
        int[] numbers = {2, 7, 11, 15};
        int target = 9;
        Solution14 solution14 = new Solution14();
        System.out.println(new Gson().toJson(solution14.twoSum(numbers, target)));
    }
}
